/*
  Tên:Nguyễn Văn Huy
  MSSV:11374891
  Lớp:DHTH7DLT
  Ngày hoàn thành:28/04/2012
  */
#include "frthemsach.h"
#include "ui_frthemsach.h"
#include <QMessageBox>
#include <QtSql>

frThemSach::frThemSach(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::frThemSach)
{
    ui->setupUi(this);
    DB =new MySqlConnection();
    DB->Open();
    loadcbtg();
    loadcbtl();
}

frThemSach::~frThemSach()
{
    delete ui;
}

void frThemSach::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
        switch (e->type()) {
        case QEvent::LanguageChange:
            ui->retranslateUi(this);
            break;
        default:
            break;
        }
}

void frThemSach::themsach()
{
    QString Tensach;
    int giaban,soluong,matacgia,matheloai;
    if(ui->txtten->text().isEmpty()||ui->txtgia->text().isEmpty()||ui->txtsoluong->text().isEmpty())
    {
        QMessageBox::critical(this,"thongbao",tr("%1 ban chua nhap du thong tin").arg(ui->cbtheloai->currentIndex()+1));
        return;
    }
    Tensach=ui->txtten->text();
    giaban=ui->txtgia->text().toInt(0,10);
    soluong=ui->txtsoluong->text().toInt(0,10);


}

void frThemSach::capnhapsach()
{
}

void frThemSach::xoasach()
{
}

void frThemSach::loadcbtg()
{
    tableModel =new QSqlRelationalTableModel(this);
    tableModel->setTable("Sach");
    tableModel->setRelation(sach_TacgiaId,QSqlRelation("TacGia","MaTacGia","TenTacGia"));
    tableModel->setSort(sach_Ten,Qt::AscendingOrder);
    QSqlTableModel *relationModel=tableModel->relationModel(sach_TacgiaId);
    ui->cbtacgia->setModel(relationModel);
    ui->cbtacgia->setModelColumn(relationModel->fieldIndex("TenTacGia"));
}

void frThemSach::loadcbtl()
{
    //DB->Open();
    QSqlTableModel *tlmodel=new QSqlTableModel();
    tlmodel->setTable("Sach");
    tlmodel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    tlmodel->select();

    //qDebug()<<tlmodel->lastError();
    ui->cbtheloai->setModel(tlmodel);
    ui->cbtheloai->setModelColumn(1);
    //ui->cbtheloai->currentIndex(0);
    //DB->Close();
}
